<template>
  <view>
    
    <!-- 搜索 -->
    <my-search bgColor="#ff00ff" radius="20" @xxx="gotoSearch"></my-search>
    
    <view class="cate-container">
      <!-- 左侧一级导航 -->
      <scroll-view class="cate-left" scroll-y :style="{height: wh+'px'}">
        <view
          :class="['cate-left-item', active === i  ? 'active' :'']"
          v-for="(item, i) in cateList"
          :key="i"
          @click="changeActive(i)"
          >
          {{item.cat_name}}
        </view>
      </scroll-view>
      
      <!-- 右侧二级导航 -->
      <scroll-view class="cate-right" scroll-y :style="{height: wh+'px'}" :scroll-top="scrollTop">
        
        <view class="cate-right-item" v-for="(item, i) in cateLev2" :key="i">
          <!-- 二级分类 title -->
          <view class="cate-right-title">
            /{{item.cat_name}}/
          </view>
          <!-- 二级分类详情 -->
          <view class="cate-right-detail">
            <view
              class="cate-right-detail-item"
              v-for="(item2, i2) in item.children"
              :key="i2"
              @click="gotoGoodsList(item2)"
            >
              <image :src="item2.cat_icon" mode="widthFix"></image>
              <text>{{item2.cat_name}}</text>
            </view>
          </view>
        </view>
        
      </scroll-view>
    </view>
  </view>
</template>

<script>
  // 导入mixin
  import badgeMixin from '@/mixins/tabbar-badge.js'
  
  export default {
    mixins: [badgeMixin],
    data() {
      return {
        // 页面状态数据
        wh: 0, // 屏幕可用高度
        active: 0, // 选中的一级导航
        scrollTop: 0, // 右侧滚动距离顶部位置
        // 页面显示数据
        cateList: [], // 分类数据
        cateLev2: [], // 二级分类数据
      };
    },
    onLoad() {
      // 发起网络请求
      this.getCateList()
      // 获取屏幕高度
      const sysInfo = uni.getSystemInfoSync()
      this.wh = sysInfo.windowHeight - 50
    },
    methods: {
      gotoSearch(e){
        // 拿到一些数据，跳转到搜索页
        console.log('====', e)
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      },
      
      // 获取分类数据
      async getCateList() {
        const {data: res} = await uni.$http.get('/categories')
        console.log(res)
        // 异常数据判断
        if(res.meta.status !== 200) return uni.$showMsg()
        // 设置页面数据
        this.cateList = res.message
        this.cateLev2 = res.message[0].children
      },
      
      changeActive(i){
        // 改变左侧一级分类选中位置
        this.active = i
        // 设置二级分类数据
        this.cateLev2 = this.cateList[i].children
        // 动态设置页面到顶距离
        this.scrollTop = Math.random()
      },
      // 跳转到商品列表页，传递 分类id
      gotoGoodsList(item){
        console.log(item)
        uni.navigateTo({
          url: `/subpkg/goods_list/goods_list?cat_id=${item.cat_id}`
        })
      }
    }
  }
</script>

<style lang="scss">
  .cate-container {
    display: flex;
    .cate-left {
      height: 500rpx;
      width: 240rpx;
      .cate-left-item{
        display: flex;
        justify-content: center;
        align-items: center;
        height: 120rpx;
        font-size: 24rpx;
        &.active {
          background-color: white;
          display: flex;
          justify-content: space-between;
          &::before {
            content: '';
            width: 4rpx;
            height: 60rpx;
            background-color: #c00000;
          }
          &::after {
            content: '';
            width: 4rpx;
            height: 60rpx;
            background-color: white;
          }
        }
      }
    }
    
    .cate-right {
      background-color: white;
      .cate-right-item {
        
        .cate-right-title {
          // background-color: red;
          height: 80rpx;
          display: flex;
          justify-content: center;
          align-items: center;
          font-weight: bold;
        }
        .cate-right-detail {
          display: flex;
          flex-wrap: wrap;
          .cate-right-detail-item {
            width: 33.33%;
            display: flex;
            padding: 8px;
            box-sizing: border-box;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            image {
              width: 100%;
            }
            text {
              font-size: 24rpx;
            }
          }
        }
      }
    }
  }
  

</style>
